package com.shelpe.services.algorithm.client;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.algorithm.client.LexiconClient.LexiconClientFallback;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import rx.Observable;

@FeignClient(name="${feign.lexiconserver.name}", url="${feign.lexiconserver.url}", fallback = LexiconClientFallback.class)
public interface LexiconClient {

	@RequestMapping(method = RequestMethod.GET,path="/lexiconservice/adgroups/{adgroup_id}/recommends",  consumes="application/json;charset=UTF-8")
	List<RecommendWordDto> smartWordsAsDevice(@PathVariable("adgroup_id") long adgroupID,
			@RequestParam(name="mode")int modeValue,
			@RequestParam(name="device")int deviceValue,
			@RequestParam(name="defaultPrice")float defaultPrice,
			@RequestParam(name="maxPrice")float maxPrice);

	@RequestMapping(method = RequestMethod.POST, path="/lexiconservice/adgroups/{adgroup_id}/records",  consumes="application/json;charset=UTF-8")
	Observable<Response> createAdgroupRecord(@PathVariable("adgroup_id")long adgroupID, @RequestBody AdgroupRecordDto dto);
	
	@RequestMapping(method = RequestMethod.POST, path="/lexiconservice/adgroups/{adgroup_id}/metrics",  consumes="application/json;charset=UTF-8")
	Observable<Response> syncAdgroupRecordMetrics(@PathVariable("adgroup_id")long adgroupID, @RequestBody AdgroupMetricDto dto);
	
	@Data
	public static class AdgroupMetricDto {
		@JsonProperty("sync_category")
		private boolean syncCategory = true;
		@JsonProperty("sync_device")
		private boolean syncDevice = true;
	}
	
	@RequestMapping(method = RequestMethod.POST, path="/lexiconservice/adgroups/{adgroup_id}/tags",  consumes="application/json;charset=UTF-8")
	Response calcAdgroupRecordTags(@PathVariable("adgroup_id")long adgroupID);
	
	@Data
	public static class AdgroupRecordDto {
		private boolean needSync;
		private boolean async;
		
		public AdgroupRecordDto(){}
		public AdgroupRecordDto(boolean needSync){
			this.needSync = needSync;
		}
	}
	
	@Data
	public static class Response{
		private long adgroupID;
		private String adgroupRecordID;
		private boolean status;
		private SimbaProxyBaseDto.Error error;
	}

	
	@Data
	public static class RecommendWordDto {
		private String word;
		
		private int pertinence;//相关性
		private int competition;//竞争度
		private int impression;//展现量
		
		@JsonProperty("pv")
		private int pageVolume;
		@JsonProperty("avg_price")
		private float averagePrice;//平均价格
		@JsonProperty("recommend_price")
		private float recommendPrice;
		private float price;
		
		private int click;//点击量
		@JsonProperty("ctr")
		private float clickRatio;//点击率
		@JsonProperty("coverage")
		private float clickCoverage;//点击转化率
		
		private boolean centeral;
		private String origin;
	}
	
	@Service
	@Slf4j
	public static class LexiconClientFallback implements LexiconClient{

		@Override
		public List<RecommendWordDto> smartWordsAsDevice(long adgroupID, int modelValue, int deviceValue, float defaultPrice, float maxPrice) {
			log.error("fallback smartWordsAsDevice: {}", adgroupID);
			return new ArrayList<RecommendWordDto>();
		}

		@Override
		public Observable<Response> createAdgroupRecord(long adgroupID, AdgroupRecordDto dto) {
			log.error("fallback createAdgroupRecord: {}", adgroupID);
			return Observable.just(new Response());
		}

		

		@Override
		public Response calcAdgroupRecordTags(long adgroupID) {
			log.error("fallback calcAdgroupRecordTags: {}", adgroupID);
			return new Response();
		}

		@Override
		public Observable<Response> syncAdgroupRecordMetrics(long adgroupID, AdgroupMetricDto dto) {
			log.error("fallback syncAdgroupRecordMetrics: {}", adgroupID);
			return Observable.just(new Response());
		}
		
	}
}
